Does the Doob-Dynkin lemma hold for any measurable space that separates points?
A characterization of spaces with the Doob-Dynkin property was given by N. Pintacuda in his paper Sul lemma di misurabilità di Doob (1989). Unfortunately, the paper is in Italian and it doesn't show up on Google.
L. Pratelli gave another proof of Pintacuda's result in his paper Sur le lemme de mesurabilité de Doob (1990). You can find this paper (in French) here. His main result is the following (the translation from French is my own, and I changed the notation and terminology to match yours):
Theorem: A measurable space $(Z, \mathcal{Z})$ has the Doob-Dynkin property if and only if it is separated and injective.
A measure space $(Z, \mathcal{Z})$ is said injective if for any space $(Y, \mathcal{Y})$ and any subset $A \subset Y$ (measurable or not), any measurable map from $A$ (with the subspace sigma algebra) to $Z$ can be extended to a measurable map from $Y$ to $Z$.
The proof of the "if" part is essentially the one you gave, and the injectivity assumption is an ad hoc way to account for the fact that $f$ might not be surjective. You first prove that there exists a map $h: f(X) \to Z$ such that $g = h \circ f$. The author shows that this map is measurable if $f(X)$ is equipped with the subspace sigma algebra. By the injectivity of $Z$, it extends to a measurable map from $Y$ to $Z$. When $f$ is assumed surjective, the proof reduces to the one you gave.
Let's say that a space $Z$ has the weak Doob-Dynkin property if it has the Doob-Dynkin property with respect to surjective maps $f$. By the discussion above, a separated space has the weak Doob-Dynkin property. In fact, I believe that the converse also holds, i.e. a space has the weak Doob-Dynkin property if and only if it is separated.
Here is a sketch of a proof, which is essentially a minor modification of Proposition (1.1) in Pratelli's paper. Suppose $(Z, \mathcal{Z})$ has the weak Doob-Dynkin property. Take $X= Z$ and $g: Z \to Z$ the identity. Following Pratelli, consider the space $\tilde{Y}$ of maps $\mathcal{Z} \to \{ 0, 1\}$ and the map $\tilde{f}: Z \to \tilde{Y}$ that sends $x \in Z$ to the dirac measure $\delta_x: \mathcal{Z} \to \{0, 1 \}$. Equip $\tilde{Y}$ with the sigma algebra $\mathcal{A}$ generated by sets of the form $$ A_{U,V} = \{ h: \mathcal{Z} \to \{ 0, 1 \} \; : \; h(U) \in V \} $$
where $U$ ranges over $\mathcal{Z}$ and $V$ ranges over the power set of $\{0, 1 \}$. Now, let $Y \subset \tilde{Y}$ be the image of $\tilde{f}$, $f: Z \to Y$ be the restriction and $\mathcal{A}_Y$ be the subspace sigma algebra on $Y$. Then $f$ is surjective and one can check that it is measurable and strict, i.e. $f^{-1}(\mathcal{A}_Y) = \mathcal{Z}$ (this is Pratelli's terminology).
By the Doob-Dynkin property, there is a measurable map $h: Y \to Z$ with $id_Z = h \circ f$. But this implies that $f$ injective. It is an easy check that this is equivalent to $Z$ being separated.
[1] MR1008597 Pintacuda, Nicolò. On Doob's measurability lemma. (Italian. English summary) Boll. Un. Mat. Ital. A (7) 3 (1989), no. 2, 237–241.
[2] MR1071531 Pratelli, Luca. Sur le lemme de mesurabilité de Doob. (French) [On Doob's measurability lemma] Séminaire de Probabilités, XXIV, 1988/89, 46–51, Lecture Notes in Math., 1426, Springer, Berlin, 1990.
Let's agree that "$g$ is a measurable function of $f$" means that there is a measurable function $h$ from $Y$ to $Z$ such that $g=h\circ f$.
The first question is answered by OP. A counter-example for the second question:
Let $Z$ be a subset of $[0,1]$ that is not analytic, so that $Z$ not a Borel measurable image of $[0,1]$ (Fremlin's Measure Theory, 423G).
$\mathcal{Z}$ is the Borel $\sigma$-algebra on $Z$, $(Y,\mathcal{Y})$ is $[0,1]$ with its Borel $\sigma$-algebra, $X=Z$, $g$ is the identity mapping from $X$ to $Z$, and $f$ is the embedding of $X$ to $Y$.
If $h$ is a mapping from $Y$ to $Z$ such that $g=h\circ f$ then $Z=h(Y)$.