This is a very old and popular question: the following link is the similar question
When is matrix multiplication commutative?
And there is a very famous theorem:
If $A,B$ are simultaneously diagonalizable $\rightarrow$ $AB=BA$
However, the reverse is not true; for example:
$A = \begin{bmatrix}0 & 1 \\1 & 0\end{bmatrix} $ and $B = \begin{bmatrix}1 & 0 \\0 & 1\end{bmatrix}$.
My question is:
Is there any necessary but not sufficient condition to have $AB=BA$?
i.e., if $AB=BA$, then this condition must hold but not vice versa.
Another question is:
If I only focus on the permutation matrix, which will be applied for the study of the commutative group, can I say any necessary condition for $AB=BA$?