First place a coordinate system to one point and express the coordinates of the 2nd point relative to the first. This way
$$\begin{aligned}
\boldsymbol{r}_A & = \pmatrix{0\\0\\0} & \boldsymbol{r}_B & = \pmatrix{X_B\\Y_B\\Z_B}
\end{aligned} $$
The difficulty here is finding the 3×3 rotation matrix $\mathbf{R}$ which transforms a 2D point $(x,y,0)$ into a 3D point $(X,Y,Z)$. But this rotation matrix is subject to certain constraints.
- Gravity needs to remain vertical (along the negative y-axis)
- The second point is somewhere in the local xy plane (have $z=0$)
This is achieved with a rotation about the Y axis of some angle $\theta$. This angle is $$ \tan \theta = \frac{Z_B}{X_B} $$ and the rotation matrix
$$ \mathbf{R} = \left| \matrix{ \tfrac{X_B}{\sqrt{X_B^2+Z_B^2}} & 0 & -\tfrac{Z_B}{\sqrt{X_B^2+Z_B^2}} \\ 0 & 1 & 0 \\ \tfrac{Z_B}{\sqrt{X_B^2+Z_B^2}} & 0 & \tfrac{X_B}{\sqrt{X_B^2+Z_B^2}}} \right| $$
and the inverse
$$ \mathbf{R}^\top = \left| \matrix{ \tfrac{X_B}{\sqrt{X_B^2+Z_B^2}} & 0 & \tfrac{Z_B}{\sqrt{X_B^2+Z_B^2}} \\ 0 & 1 & 0 \\ -\tfrac{Z_B}{\sqrt{X_B^2+Z_B^2}} & 0 & \tfrac{X_B}{\sqrt{X_B^2+Z_B^2}}} \right| $$
So now the 2D to 3D transformation is
$$ \pmatrix{X\\Y\\Z} = \mathbf{R} \pmatrix{x\\y\\0} $$
and the 3D to 2D problem
$$ \pmatrix{x\\y\\0} = \mathbf{R}^\top \pmatrix{X\\Y\\Z} $$