Suppose I have a theory $A$ concerning the evolution of a set of fields $T_1, \dots, T_n$. Let the action functional for this theory be $S[T_1, \dots, T_n]$. Suppose in the action, in addition to possible other functions, there is a function $f(T_i, \dots, T_{i + j})$ of a subset of the fields. Finally, suppose the variation of $S$ gives an explicit form for $f$, say $f = g$. My question: If we substitute $g$ for $f$ in the action $S$, does the action still describe the theory $A$?
As a particular example, consider the Einstein-Hilbert action with a matter action $$ S = \frac{1}{\kappa^2} \int d^4x \sqrt{-g}R + S_m.\tag{1} $$ Variation yields the Einstein field equation (EFE) $R_{\mu\nu} = \kappa^2 T_{\mu\nu} + \frac{1}{2}R g_{\mu\nu}$, whose trace tells us that $R = -\kappa^2 T$, where $T \equiv T^\mu_\mu$. If we substitute this into $(1)$ above, we obtain the action $$ S = -\int d^4x \sqrt{-g}T + S_m.\tag{2} $$ My question: does this action still describe GR? I think it should because the action $(2)$ ought to be an extremum precisely when $(1)$ is, but I still have my doubts since the only curvature coupling inherent in the theory $(2)$ is strictly to the metric and none whatever to the Ricci scalar.