Here is a macro that I think will do what you want.
\def\textUnderline#1{#1\llap{\style{padding-bottom: 7px; border-bottom: 2px solid}{\phantom{\smash{#1}}}}}
or
MathJax = {
tex: {
macros: {
textUnderline: ['{#1\\llap{\\style{padding-bottom: 7px; border-bottom: 2px solid}{\\phantom{\\smash{#1}}}}}', 1],
}
}
};
For your example (substituting \textUnderline for \underline, though you could redefine \underline if you prefer) produces
Note, however, that if the math extends far below the baseline (which is often the case in mathematics, but perhaps not in what you are going to be using), then the underline will extend over it, as in
<span class="red underline">Hi$\textUnderline{\text{Hi}Hi{\color{blue}\Bigg(}}$</span>
produces
If you wanted to have the blue parenthesis be above the red underline, then use \rlap rather than \llap and move the #1 to the end of the macro instead.
Davide