Thanks to @Reinderien in the comments for the solution idea. Scipy has a solve_sylvester
function specifically for this problem, namely,
import scipy
X = scipy.linalg.solve_sylvester(A, -A, B)
This could use optimizations for my case, but it will work well enough. Unfortunately, the function does not seem to support batching.