This should work (and if you put triple backticks “```” at the top and bottom of your code, it will be better formatted).
The general problem of 2d->3d is whether it is prohibitively expensive in terms of memory (or compute), which could be the case if the total number of voxels is very large.
Best regards
Thomas