Let $(X,Y,Z)$ be three random variables such that $X$ is not independent of $Y$, $X$ is not independent of $Z$, while $Y$ is independent of $Z$. Is it possible to construct a function $f:\mathbb{R}^2\to\mathbb{R}$ such that $f(X,Y)$ is independent of $Z$, yet $f$ is different from $f(X,Y)=g(Y)$ for some $g:\mathbb{R}\to\mathbb{R}$?
I have difficulties finding a counterexample. If the answer is positive, I'm interested in finding the largest possible class of functions/distributions for which this is not the case.
Let $X\sim \mathcal N (0,1)$. $Y= \vert X \vert 1_{\vert X \vert \leq 1}$. $Z=\text{sgn} (X)$. $f(x,y) := \vert x \vert$. Then $\vert X\vert = f(X,Y)$ is independent from $Z$. It can't hold $g(Y) = \vert X\vert$ for some $g$, because $g(Y) = g(0)$ with positive probability, but $\Bbb P ( \vert X \vert = g(0) ) = 0$