Ensure all file objects are closed when reading/writing HDF5

This fixes file objects that were not explicitly closed in the HDF5 code and tests. This was identified with the testing framework from #673.

This is fairly trivial, but I wanted to submit it as a PR to ensure it gets backported.