There are 3 points in space, B and C that define a line and a third point D that is not on the line. How to find the angles by which to rotate the line on each of the axis so that point D belongs on the line? I've managed to solve this in 2 dimensions but I'm having trouble translating that to 3D.

The equation I've found is: $$\alpha=-\beta+180°-\gamma-\arcsin\left(\frac{h\cdot\sin\gamma}{i}\right)$$
Where $\alpha$ is the angle of rotation, $\beta$ is the angle DOB, $\gamma$ is the angle OBC, h is the length of the segment OB and i is the length of the segment OD.
My attempt at making this work in 3D was to project the points B and C onto the XY plane and calculate the Z angle, then rotate the points around the Z axis by the angle I found, project the rotated points onto the XZ plane and find the Y angle and repeat for the X angle.
This doesn't seem to work however. Is there a better way of going about this?
Suppose instead of rotating the line passing through $B$ and $C$ so that point $D$ lies on the rotated line, we'll go the other way around, and rotate point $D$ about an axis passing through the origin, so that $D$'s image lies on the line $BC$. After we find this rotation, we'll just invert the rotation and apply it to line $BC$, to make it contain point $D$.
Since the rotation of point $D$ about any axis passing through the origin, will be another point that lies on the sphere centered at the origin, and with a radius equal to $OD$, then all we have to do to find the image of point $D$, is intersect the sphere with line $BC$. There can be no intersections, one intersection, or two intersections at most. If there are no intersections, then it is not possible to perform this rotation to satisfy the given condition. Otherwise, pick an intersection, call it $D'$ as the image of $D$. Now we want to rotate $OD$ into $OD'$, and this can be done in an infinite number of ways, as the axis of rotation is not unique, but instead lies in the plane whose normal vector is $DD'$ and passing through the midpoint of $DD'$.
However, there is a rotation that uses the minimum possible rotation angle, and that rotation has an axis given by $OD \times OD'$, and the rotation angle $\theta$ is given by
$ \theta = \text{atan2}\bigg( \dfrac{OD \cdot OD'}{ \| OD \|^2 } ,\dfrac{ \| OD \times OD' \| } { \| OD \|^2 } \bigg) = \text{atan2} ( OD \cdot OD' , \| OD \times OD' \| ) $
Where $\text{atan2}(x, y) = \phi$, with $\cos \phi = \dfrac{x}{\sqrt{x^2 + y^2}}, \sin \phi = \dfrac{y}{\sqrt{x^2+ y^2} } $
Having found the axis and the angle that sends $D$ to $D'$, in order to answer your question, we just apply the reverse rotation to line $BC$ to rotate it into $B'C'$ and by our construction, point $D$ will lie on it.
If rotation about an arbitrary axis was allowed, then we'll have to use the Rodrigues' rotation matrix formula which states that the rotation matrix is given by
$ R(\theta) = {a a}^T + (I - {aa}^T) \cos \theta + S_a \sin \theta $
where $a$ is the unit vector along the axis of rotation, $\theta$ is the angle of rotation counter-clockwise with the axis pointing towards you, and $S_a$ is given by
$ S_a = \begin{bmatrix} 0 && - a_z && a_y \\ a_z && 0 && -a_x \\ -a_y && a_x && 0 \end{bmatrix} $
However, the question asks for a rotation that is a composition of rotations about the $x$, $y$ , and $z$ axes.
To perform the rotation from $D$ to $D'$ using rotations about the $x$, $y$ and $z$ axes, let
$ D = ( d_x , d_y, d_z ) $
$ D' = (d'_x, d'_y, d'_z ) $
If $ |d_z| \ge |d'_z|$, then by rotating $D$ about the $x$ axis by an angle $\phi$ we obtain
$ D_1 = ( d_x , \cos \phi d_y - \sin \phi d_z, \sin \phi d_y + \cos \phi d_z ) $
We can select $\phi$ such that
$ \sin \phi d_y + \cos \phi d_z = d'_z $
Now, we have
$ D_1 = ( d_x , \cos \phi d_y - \sin \phi d_z , d'_z ) = (d_x, d_{1y}, d'_z) $
So that $D_1$ and $D'$ have the same $z$-coordinate. Therefore, now we can simply rotate $D_1$ about the $z$ axis into the vector $D_2$, by an angle $\psi$, such that $D_2$ and $D'$ have the same coordinates.
$D_2 = ( \cos \psi d_x - \sin \psi d_{1y} , - \sin \psi d_x + \cos \psi d_{1y} , d'_z ) $
i.e. we have to find $\psi $ such that
$ \cos \psi d_x - \sin \psi d_{1y} = d'_x $ , and
$ \sin \psi d_x + \cos \psi d_{1y} = d'_y $
which is a linear system in $\cos \psi$ and $\sin \psi$ and can be readily solved.
Similar set up can be used if $ |d_x| \ge |d'_x| $ where we rotate $D$ first with respect to $z$ axis, which brings the $x$ coordinates to be the same, then we apply a rotation about $x$ axis to make all coordinates the same. And finally, if both $ |d_x| \lt |d'_x| $ and $|d_z| \lt |d'_z| $ then it must be the case that $ |d_y| \gt |d'_y|$, so we rotate about the $x$ axis to bring the $y$ coordinates to be equal, then we rotate about the $y$ axis to make all coordinates equal.