As mentioned in the comments, \mathrm{f} will insert a letter from the text font as a math symbol. Here’s an example set in ISO style in legacy NFSS mode. The constants i, e and π are all upright, but the variable x is italic. You’ll notice that the upright letters are the same as from the text font. It would be possible to select another font as \mathrm, for example with isomath.
\documentclass[varwidth, preview]{standalone}
\usepackage[T1]{fontenc}
\usepackage{textcomp, amssymb}
\usepackage[utf8]{inputenc}
\usepackage{mathtools}
\newcommand\upe{\mathord{\mathrm{e}}}
\newcommand\upi{\mathord{\mathrm{i}}}
\newcommand\uppi{\mathord{\mathrm{\pi}}}
\begin{document}
\begin{align*}
\upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
\upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

The unicode-math package distinguishes between text letters in math mode (\mathrm) and upright math symbols (\symup). However, you need to change the upright math alphabet to notice any difference in appearance. Here is an example that replaces the upright math symbols with CMU Serif Upright Italic, an unslanted version of the Computer Modern italic font. (Except for the digits, because $10$ and 10 should stay the same.) Notice that unicode-math is able to tell the difference between upright math symbols and operator names.
\documentclass[varwidth, preview]{standalone}
\usepackage{mathtools}
\usepackage[math-style=ISO]{unicode-math}
\setmathfont{Latin Modern Math}
\setmathfont[range=up/{Latin,latin,Greek,greek}]{CMU Serif Upright Italic}
\newcommand\upe{\symup{e}}
\newcommand\upi{\symup{i}}
\begin{document}
\begin{align*}
\upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
\upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

I think the result matches pretty well, but Knuth’s Computer Modern italic font was never intended to be unslanted. (The tail of the e did not originally stick out like that.) So here’s another example, which combines Asana Math with upright math letters from Neo Euler. These are clones of two famous fonts by Hermann Zapf (Palatino and AMS Euler) that complement each other very nicely. The text font (also used for operator names) is another clone of Palatino.
\documentclass[varwidth, preview]{standalone}
\usepackage{mathtools}
\usepackage[math-style=ISO]{unicode-math}
\setmainfont{TeX Gyre Pagella}
\defaultfontfeatures{Scale=MatchLowercase}
\setmathfont{Asana Math}
\setmathfont[range={up/{Latin,latin,Greek,greek},
bfup/{Latin,latin,Greek,greek}},
script-features={}, sscript-features={}
]{Neo Euler}
\newcommand\upe{\symup{e}}
\newcommand\upi{\symup{i}}
\begin{document}
\begin{align*}
\upe^{\upi x} &= \cos{x} + \upi \sin{x} \\
\upe^{\upi \uppi} + 1 &= 0
\end{align*}
\end{document}

\mathrm{f}(also if you are using latex don't use$$) – David Carlisle May 19 '18 at 22:55$$? – RothX May 19 '18 at 22:57\textwould be wrong, that is for a nested insert of the surrounding text, not for a math variable that needs to be in a specific font. Notably in a theorem, if the surrounding text is italic\textwould be italic – David Carlisle May 20 '18 at 07:57\textis wrong. – egreg Oct 05 '23 at 16:35