Hmmmm… Does this related question help? Pass two-dimensional array of matrices into RStan
It looks like the issue is that in R it’s natural to have the last index vary slowest, making it the preferred way to store arrays with k matrices of size (m, n) as some object of size (m, n, k). I don’t know whether there is a way around having to adapt this part of the R code to Stan’s way, which is storing these matrices as some object of size (k, m, n). In Python (which I use) this is how I would do it anyways, so there is no issue here.
Something like this surely comes up regularly, maybe there exists a good solution somewhere.