How to implement a text "within" an arrow in math mode?

I propose this:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\mytextarrow}[1]{\mathrel{\relbar\mkern-10mu\relbar\mkern-3.5mu\raisebox{0.25ex}{\scriptsize#1}\!\rightarrow}}

\begin{document}

\begin{equation*}
2 + 2 \mytextarrow{goes to} 4
\end{equation*}

\end{document} 

enter image description here


I'd use smaller type vertically centered with respect to the math axis:

\documentclass{article}
\usepackage{amsmath}

\newcommand{\crightarrow}[1]{%
  \relbar\joinrel\joinrel\relbar\mathrel{\vcentertext{#1}}\rightarrow
}

\newcommand{\vcentertext}[1]{%
  \vcenter{\hbox{\scriptsize\smallstrut#1}}%
}
\newcommand{\smallstrut}{\vrule height 1.5ex depth 0.5ex width 0pt }

\begin{document}

$a_n \crightarrow{goes to} a$

$a_n \crightarrow{oes} a$

$a_n \crightarrow{bbb} a$

$a_n \crightarrow{ppp} a$

\end{document}

enter image description here


\usepackage{tikz}
\usetikzlibrary{arrows.meta}

\newlength\tmplength
\newcommand*\textarrow[2][3em]
  {\mathrel{\vcenter{\hbox{\settowidth\tmplength{\scriptsize#2}%
                           \tikz[font=\scriptsize]
                             \draw[->](0,0)--node[fill=white,midway]{#2}
                             ++({#1+\the\tmplength},0);}}}}

and you can use

$a \textarrow{foo} b$
$a \textarrow[5em]{bar} b$
$a \textarrow[10em]{baz} b$

enter image description here