I am working on an optimization problem related to a unitary matrix $X$, and wonder whether it is possible to encode a unitary matrix using a set of independent variables? For instance, for a 2D unitary matrix, it is easy to show that it can be encoded with a single variable $\theta$ as
$$X=\begin{bmatrix}\cos\theta & -\sin\theta\\\sin\theta & \cos\theta\end{bmatrix}$$
but I am not sure how to generalize this to any N-D matrix? Any information would be much appreciated, thank you!
Partial answer
A unitary matrix is given by the image of the canonical basis $\{e_1, \dots, e_n\}$ which is an orthonormal basis $\{u(e_1), \dots, u(e_n)\}$
Using spherical coordinates, the image $u(e_1)$ of $e_1$ can be described with the angles $\phi_1^1, \dots, \phi_n^1$. Then the image $u(e_2)$ of $e_2$ lie in an hyperplane orthogonal to $u(e_1)$. It can be described with the angles $\phi_1^2, \dots, \phi_{n-1}^2$.
You can follow on that way until you define $u(e_n)$, with two choices depending on the sign of the determinant of $u$.
Additional comment
You're not right when you say that any unitary $2$-d matrix can be represented by a rotation. You also have unitary matrices with determinant equal to minus one which are not rotations.