You could use sessions if you don't feel like creating a function (
Before closing your splits simple do
This will save the current layout in the file
~/tmp.session (of course you can change the file path)
Then expand your window as you usually do and then use:
which will restore the layout saved in the file.
Also you can even simplify it, knowing that without argument
:mksession will create the file
~/Session.vim you can do the following:
nnoremap <key> :mksession<CR> :only<CR>
This mapping will save your session and "maximize" the current window.
Then create this mapping:
nnoremap <key> :source ~/Session.vim<CR>
to easily get your session back.
EDIT In the comments HuStmpHrr mentionned an issue with
NERDTree. Actually the problem comes from the way
NERDTree creates its buffers: from what I have understood it actually creates a buffer, do some stuff, delete this buffer and create another one and do some stuff again (see this nobe4 answer mentionning this).
In my opinion the wiser is to do more or less like in this answer: closing NERDTree before making a session and opening it after restoring it.
A workaround could then be to create these functions:
execute "mksession! " . a:filePath
execute "source " . a:filePath
And to use them instead of the built-in ones. That is neither clean or elegant but that could do the job.