[guided]We must connect two notions of invariance. The Hilbert-space limit from the mean ergodic theorem lands in
\begin{align*}
\mathcal H_1=\{g\in L^2(X,\mathcal B,\mu):g\circ T=g \text{ in } L^2\},
\end{align*}
whereas conditional expectation onto $\mathcal I$ is characterized among $\mathcal I$-measurable functions. Let
\begin{align*}
\mathcal K:=L^2(X,\mathcal I,\mu).
\end{align*}
We prove that $\mathcal H_1=\mathcal K$.
[claim:The fixed-vector subspace equals $L^2(X,\mathcal I,\mu)$]
One has $\mathcal H_1=\mathcal K$.
[/claim]
[proof]
Let $\mathscr Q$ be the countable basis of $\mathbb C$ consisting of open balls with centers in $\mathbb Q+i\mathbb Q$ and positive rational radii. This basis is used because it is countable and separates points: if $z\neq w$ in $\mathbb C$, then some $Q\in\mathscr Q$ contains one of $z,w$ and not the other.
First take an $\mathcal I$-measurable representative
\begin{align*}
h:X&\to\mathbb C
\end{align*}
of an element of $\mathcal K$. For each $Q\in\mathscr Q$, the preimage $h^{-1}(Q)$ belongs to $\mathcal I$. By the definition of $\mathcal I$,
\begin{align*}
T^{-1}(h^{-1}(Q))=h^{-1}(Q).
\end{align*}
Since $T^{-1}(h^{-1}(Q))=(h\circ T)^{-1}(Q)$, we get
\begin{align*}
(h\circ T)^{-1}(Q)=h^{-1}(Q)
\end{align*}
for every $Q\in\mathscr Q$. Because $\mathscr Q$ separates points, $h(Tx)=h(x)$ for every $x\in X$. Thus the $L^2$ class of $h$ lies in $\mathcal H_1$, proving $\mathcal K\subseteq\mathcal H_1$.
Now take an element of $\mathcal H_1$ and choose a finite $\mathcal B$-measurable representative
\begin{align*}
g:X&\to\mathbb C .
\end{align*}
The equality $Ug=g$ in $L^2$ means that
\begin{align*}
N:=\{x\in X:g(Tx)\neq g(x)\}
\end{align*}
has $\mu(N)=0$. For $Q\in\mathscr Q$, set
\begin{align*}
E_Q:=g^{-1}(Q).
\end{align*}
If $x\notin N$, then $g(Tx)=g(x)$, so $x\in T^{-1}E_Q$ exactly when $x\in E_Q$. Therefore
\begin{align*}
T^{-1}E_Q\triangle E_Q\subseteq N,
\end{align*}
and hence
\begin{align*}
\mu(T^{-1}E_Q\triangle E_Q)=0.
\end{align*}
For $n\geq 0$, let $T^n:X\to X$ be the $n$-fold iterate, with $T^0=\operatorname{id}_X$, and write $T^{-n}E=(T^n)^{-1}(E)$. Since $T$ is measure-preserving, every iterate $T^n$ is measure-preserving. The induction step is the inclusion
\begin{align*}
T^{-(n+1)}E_Q\triangle E_Q
\subseteq
T^{-n}(T^{-1}E_Q\triangle E_Q)\cup (T^{-n}E_Q\triangle E_Q).
\end{align*}
The first set on the right has measure zero because $T^n$ is measure-preserving and $T^{-1}E_Q\triangle E_Q$ has measure zero; the second has measure zero by the induction hypothesis. Hence
\begin{align*}
\mu(T^{-n}E_Q\triangle E_Q)=0
\end{align*}
for every $n\geq 0$.
We now replace the almost-invariant set $E_Q$ by an exactly invariant set. Define
\begin{align*}
A_Q:=\bigcap_{m=0}^{\infty}\bigcup_{n=m}^{\infty}T^{-n}E_Q .
\end{align*}
This is the set of points whose forward orbit visits $E_Q$ infinitely often. It is exactly invariant because
\begin{align*}
T^{-1}A_Q
&=
\bigcap_{m=0}^{\infty}\bigcup_{n=m}^{\infty}T^{-(n+1)}E_Q \\
&=
\bigcap_{m=1}^{\infty}\bigcup_{n=m}^{\infty}T^{-n}E_Q \\
&=
\bigcap_{m=0}^{\infty}\bigcup_{n=m}^{\infty}T^{-n}E_Q
=
A_Q.
\end{align*}
Thus $A_Q\in\mathcal I$.
It remains to check that $A_Q$ represents the same measurable set as $E_Q$ modulo null sets. Define
\begin{align*}
N_Q:=\bigcup_{n=0}^{\infty}(T^{-n}E_Q\triangle E_Q).
\end{align*}
The preceding paragraph gives $\mu(N_Q)=0$. If $x\notin N_Q$, then membership in every $T^{-n}E_Q$ agrees with membership in $E_Q$. Therefore $x\in A_Q$ exactly when $x\in E_Q$, so $A_Q\triangle E_Q\subseteq N_Q$. Hence $E_Q$ differs by a null set from the invariant set $A_Q$.
Let $\overline{\mathcal I}^{\mu}$ be the $\mu$-completion of $\mathcal I$. The collection
\begin{align*}
\mathscr S:=\{C\in\mathcal B(\mathbb C):g^{-1}(C)\in \overline{\mathcal I}^{\mu}\}
\end{align*}
is a $\sigma$-algebra. Since every basis set $Q\in\mathscr Q$ belongs to $\mathscr S$, and $\mathscr Q$ generates $\mathcal B(\mathbb C)$, we have $\mathscr S=\mathcal B(\mathbb C)$. Thus $g$ is measurable with respect to the completed invariant $\sigma$-algebra. The measurable modification lemma for completed sub-$\sigma$-algebras applies because $\mathbb C$ is a standard Borel space; it gives an $\mathcal I$-measurable map
\begin{align*}
g_{\mathcal I}:X&\to\mathbb C
\end{align*}
with $g_{\mathcal I}=g$ $\mu$-a.e. Therefore the $L^2$ class of $g$ belongs to $\mathcal K$, proving $\mathcal H_1\subseteq\mathcal K$.
[/proof]
Since $Pf\in\mathcal H_1$ by construction of the orthogonal projection, the equality $\mathcal H_1=\mathcal K$ gives
\begin{align*}
Pf\in L^2(X,\mathcal I,\mu).
\end{align*}[/guided]