File:
\documentclass[11pt]{article}
\usepackage{amsmath}
\newcommand{\argghhh}{\relax\ifmmode\bar\else\expandafter\=\fi}
\begin{document}
$ \argghhh a $
\end{document}
produces error (but NOT if \usepackage{amsmath} is commented out):
LaTeX Warning: Command \= invalid in math mode on input line 7.
! Please use \mathaccent for accents in math mode.
\add@accent ...@spacefactor \spacefactor }\accent
#1 #2\egroup \spacefactor ...
l.7 $ \argghhh a
$
Clearly this is math mode. But it chooses
(1) \=, hence NOT math mode, then
(2) complains about \= in math mode!