All alignment display environment of amsmath are defined in terms of the primitive \halign with suitable templates for the column cells.
By design, as soon as TeX starts examining a new cell in an \halign it is in “restricted horizontal mode” and recursively expands the first token it finds, in order to see whether \omit appears at the beginning, which would trigger not to use the specified template; usually, \omit is followed by \span in order to merge cells, but that's not mandatory.
In your case, TeX expands \ifmmode and this conditional turns out to be false, because TeX is not in math mode. It would only be true after the u part (what should go before the cell text) has been inserted.
Add \relax in front of \ifmmode. But of course your conditional text would be inserted by a macro: use \DeclareRobustCommand for defining it.
Thus either
\newcommand{\mymacro}{\relax\ifmmode Math\else Text\fi}
or
\DeclareRobustCommand{\mymacro}{\ifmmode Math\else Text\fi}
The latter is preferable if \mymacro can also appear in moving arguments. It works because a macro defined by \DeclareRobustCommand expands to something preceded by \protect and, in typesetting contexts, \protect is the same as \relax.
As an aside, for macros without argument using \newcommend or \newcommand* is irrelevant. The difference shows only for macros with arguments, where the *-form disallows \par (or empty lines) in any argument. The same applies to \DeclareRobustCommand.
\ifmmodeis always false. Add\relaxin front of it. – egreg Jul 14 '22 at 12:03\protectedto\def) works if\ifmmodeis hidden inside the macro, and, of course, that question itself specifically mentions both anarrayand a macro, therefore being not a duplicate of my original post. – Jul 14 '22 at 12:53