Seems making commands robust by defining via \NewDocumentCommand instead of \newcommand does the trick.
\documentclass{article} %\usepackage{xparse} \usepackage{xstring} \NewDocumentCommand{\Prob}{m}{% \IfEqCase{#1}{% {1}{p}% {2}{q}% {3}{r}% }[{#1}]% }% \NewDocumentCommand{\RandomVar}{m}{% \IfEqCase{#1}{% {1}{X}% {2}{Y}% {3}{Z}% }[{#1}]% }% \begin{document} $\Prob1$, $\Prob2$, $\Prob{p_{X,Y}}$, $p_{\RandomVar1, \RandomVar2}$ $\Prob{p_{\RandomVar1,\RandomVar2}}$ \end{document} The last line yields p_{\RandomVar1,\RandomVar2} which in turn yields p_{X,Y}.
A better approach might be using expandable \str_case:nnTF:
\documentclass{article} \ExplSyntaxOn \cs_new:Npn \Prob #1 {% % \exp_args:Ne fully expands the argument passed to \str_case:nnTF. \exp_args:Ne \str_case:nnTF {#1} { {1} {p} {2} {q} {3} {r} } {} {#1} } \cs_new:Npn \RandomVar #1 {% % \exp_args:Ne fully expands the argument passed to \str_case:nnTF. \exp_args:Ne \str_case:nnTF {#1} { {1} {X} {2} {Y} {3} {Z} } {} {#1} } \ExplSyntaxOff \begin{document} $\Prob1$, $\Prob2$, $\Prob{p_{X,Y}}$, $p_{\RandomVar1, \RandomVar2}$ $\Prob{p_{\RandomVar1,\RandomVar2}}$ \end{document} 