Here is an MWE of the problem at hand:
\documentclass{article}
\usepackage{chemformula}
\begin{document}
$\varphi_{\ch{N2}}$%chemformula as index works
\ch{C + O2 + N2 -> CO2 + N2}%regular equation
\ch{C + O2 + $\varphi$ N2 -> CO2 + N2}%escaped math works
%\ch{C + O2 + $\varphi_{\ch{N2}}$ N2 -> CO2 + N2}%throws error when nested
\end{document}
In the code with its comments, the problem is explained: using chemformula and its command \ch, indices may be created, even in math mode, see the first line of code.
Formulating a longer equation within \ch but then also trying to escape math symbols who themselves have indices typeset using \ch results in nested \ch commands. These throw and error
TeX capacity exceeded, sorry [main memory size=3000000]. ...+ O2 + $\varphi_{\ch{N2}}$ N2 -> CO2 + N2}
when compiled.
How do I solve this?

\chcalls\chemformula_ch:nn, which parses the formula and stores it into\g__chemformula_output_tlbefore using that global variable. Parsing the big formula puts\__chemformula_ch_main:n {C + O2 + $\varphi _{\ch {N2}}$ N2 -> CO2 + N2}in the global variable. Among other things this runs\ch{N2}, which adds\__chemformula_ch_main:n {N2}to the global variable. When the variable is run it contains two\__chemformula_ch_main:n. The first includes\ch{N2}, which adds a third\__chemformula_ch_main:nand runs the variable... Sorry, I won't have time to dig more. – Bruno Le Floch Oct 06 '18 at 12:53