More robust check in dimshuffle pattern