You need to char() the string matrix into a char array of constant width, and then you write the 2D char array.
using char() on a string array will blank pad the ends of shorter lines. If some of your strings end in whitespace that are important to preserve, then the opposite operation, string(cellstr(TheCharacterArray)) will trim those blanks out.
If that is not acceptable, you would need to choose a terminator character that you are certain will not occur otherwise, and put it on the end of shorter lines. Something like
S=["test1";"test2";"test567"];
Sc = char(S+'~');
Sc(:,end) = [];
and reverse would be
S_rebuilt = string(regexprep(cellstr(Sc), '~.*$', ''));
This is only needed if you have trailing whitespace you need preserved. Otherwise:
Sc = char(S);
S_rebuilt = string(cellstr(Sc));
Be careful, by the way, as to which dimension gets written first when you write a 2D array of char. You might need to write Sc.' (transpose) to match the standard use inside netcdf, and you might need to transpose when you read it in again,
S_rebuilt = string(cellstr(Sc.'));